﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MISAPP.Classify.DataAccess;
using System.Data;
using MISAPP.MISInterface;

namespace MISAPP.Classify.Service
{
    public class Service_WMG005
    {
        
        public enum DepositOpt { Deposit=1,Savings,All };

        DataTable dt = null;
        DA_WMG005 da = null;
        protected DA_WMG005 DA
        {
            get
            {
                if (da == null)
                {
                    da = new DA_WMG005();
                }
                return da;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_criteria"></param>
        /// <param name="p_opt">1:定期存款2:定期儲蓄</param>
        /// <returns></returns>
        public DataTable GetSummaryData(string p_criteria, DepositOpt p_opt)
        {
            if (p_opt == DepositOpt.All)
            {
                throw new Exception("DepositOpt ERROR！不能選ALL");
            }

            //string ym, ymd;
            //ym = p_criteria; //EX:201104
            //ymd = string.Format("{0}-{1}-01", ym.Substring(0, 4)
            //    , ym.Substring(4));
            //DateTime d1 = Convert.ToDateTime(ymd).AddMonths(1);    

            DateTime d1 = getYMD(p_criteria, 1);
            dt = DA.GetSummaryData(p_criteria, d1.ToShortDateString()
                , Convert.ToInt16(p_opt));

            return dt;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_ym">YYYYMM</param>
        /// <param name="p_month">要加的月份</param>
        /// <returns></returns>
        protected DateTime getYMD(string p_ym, int p_month)
        {
            string  ymd;
            ymd = string.Format("{0}-{1}-01", p_ym.Substring(0, 4)
                , p_ym.Substring(4));
            DateTime d1 = Convert.ToDateTime(ymd).AddMonths(p_month);
            return d1;
        }

        /// <summary>
        /// 給外幣使用
        /// </summary>
        /// <param name="p_criteria">YYYYMM</param>
        /// <param name="p_cur">幣別EX:USD，空值表示全幣別</param>
        /// <returns></returns>
        public DataTable GetSummaryData(string p_criteria, string p_cur)
        {           
            DateTime d1 = getYMD(p_criteria, 1);
            dt = DA.GetSummaryData(p_criteria, d1.ToShortDateString(),p_cur);
            return dt;
        }

        public string[] GetCriteriaArray()
        {
            return DA.GetDistinctField();
        }
        public DataSet GetCriteriaData()
        {
            return DA.GetCriteria();
        }

        public IQueryable<string> GetDistinctDueDate()
        {
            return DA.GetDistinctDueDate();
        }

        public IQueryable<DataRow> GetSelectRow(string p_dueDate)
        {
            return DA.GetSelectRow(p_dueDate);
        }

        public IQueryable<DataRow> GetSelectRow(string p_dueDate,string p_rate)
        {
            return DA.GetSelectRow(p_dueDate,p_rate);
        }

        public string[] GetColumns(DepositOpt p_opt)
        {
            string[] columns = null;
            if (p_opt.Equals(DepositOpt.Deposit))
            {
                columns = new[] {"","", "C","D","E","F","G","H","I","J"
                            ,"K","L","M","N","O","P","R" };
            }
            else
            {
                columns = new[] {"","", "C","D","E","F","G","H","J"};
            }
            return columns;
        }

        public string[] GetCURColumn()
        {
            return new string[] {"A","B","C","D","E","F","G","H","I"
                ,"J","K","L","M","N","P" };
        }

        /// <summary>
        /// 填入Cell 上的月份
        /// </summary>
        /// <param name="p_ym"></param>
        /// <returns></returns>
        public string GetCellYM(string p_ym)
        {
            return string.Format("月份：{0}年{1}月份", p_ym.Substring(0, 3)
                , p_ym.Substring(3));
        }

        public DataTable GetTrendData(string p_ym)
        {
            dt = null;
            if (Utility.IsDate(p_ym).Equals(false))
            {
                throw new Exception("日期格式錯誤！！");
            }
            string ymd;
            ymd = string.Format("{0}-{1}-01", p_ym.Substring(0, 4)
                , p_ym.Substring(4));
            DateTime d1 = Convert.ToDateTime(ymd).AddMonths(-11);
            ymd = string.Format("{0}{1}",d1.Year.ToString(),
                d1.Month.ToString().Substring(0,1).Equals("0") 
                ? d1.Month.ToString()
                :string.Format("0{0}", d1.Month.ToString()));
            string[] trends = { ymd,p_ym };
            dt = DA.GetTrendData(trends, d1);
            return dt;
        }

        public Dictionary<string, string> GetChartColumnDict()
        {
            return new Dictionary<string, string>
            { 
                {"Year","string"}
                ,{"固定","number" }
                ,{"機動","number" }
            };
        }

        public string GetChartRow()
        {
            return dt==null ? "0"
                : (dt.Columns.Count - 2).ToString();
        }

        public DataTable GetChartData(DepositOpt p_opt)
        {
            string category = "定期+定儲";
            switch (p_opt.ToString())
            {
                case "Deposit":
                    category = "定期";
                    break;
                case "Savings":
                    category = "定儲";
                    break;
             }
            return DA.GetChartData(category);
        }
    }
}
